package baidu;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class HttpClientApache {
	CloseableHttpClient httpclient = HttpClients.createDefault();
	public HttpRespons sendGet(String urlString) throws IOException {
		HttpGet httpGet = new HttpGet(urlString);
		CloseableHttpResponse response1 = httpclient.execute(httpGet);
		return this.makeContent(urlString, response1);
	}
	public HttpRespons sendPost(String urlString, Map<String, String> params) throws IOException {
		HttpPost httpPost = new HttpPost(urlString);
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		for (String key : params.keySet()) {
			nvps.add(new BasicNameValuePair(key, params.get(key)));
		}
		httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
		CloseableHttpResponse response = httpclient.execute(httpPost);
		return this.makeContent(urlString, response);
	}
	private HttpRespons makeContent(String urlString, CloseableHttpResponse response) throws IOException {
		HttpRespons httpResponser = new HttpRespons();
		InputStream in = response.getEntity().getContent();
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		httpResponser.contentCollection = new Vector<String>();
		byte[] buffer = new byte[1024 * 10];
		int total = 0;
		while ((total = in.read(buffer)) != -1) {
			bos.write(buffer, 0, total);
		}
		String ecod = "utf-8";
		Header ecodh = response.getEntity().getContentEncoding();
		if (ecodh != null)
			ecod = "utf-8";// ecodh.getValue();
		httpResponser.urlString = urlString;
		httpResponser.protocol = response.getProtocolVersion().getProtocol();
		httpResponser.content = urlToString(bos.toString(ecod));
		httpResponser.data = bos.toByteArray();
		httpResponser.contentEncoding = ecod;
		httpResponser.contentType = response.getEntity().getContentType().getValue();
		response.close();
		return httpResponser;
	}
	public Map<String, String> formdata(String user, String pwd, String content) {
		Map<String, String> result = new LinkedHashMap<String, String>();
		Document doc = Jsoup.parse(content);
		Elements mods = doc.getElementsByClass("mod-content");
		if (mods.size() != 1) {
			try {
				content = this.sendGet("http://wappass.baidu.com/wp/login?uname_login=1").content;
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			doc = Jsoup.parse(content);
			mods = doc.getElementsByClass("mod-content");
		}
		Elements list = mods.get(0).getElementsByTag("input");
		for (Element e : list) {
			if (e.attr("name").equalsIgnoreCase("username")) {
				result.put("username", user);
			} else if (e.attr("name").equalsIgnoreCase("password")) {
				result.put("password", pwd);
			} else
				result.put(e.attr("name"), e.attr("value"));
		}
		return result;
	}
	public static String urlToString(String source) {
		int index = 0;
		while (index != -1) {
			index = source.indexOf("&#x");
			if (index != -1) {
				int b1 = Integer.decode("0x" + source.substring(index + 3, index + 5));
				int b2 = Integer.decode("0x" + source.substring(index + 5, index + 7));
				try {
					String rep = new String(new byte[]{(byte) b1, (byte) b2}, "unicode");
					source = source.replace(source.substring(index, index + 8), rep);
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
		}
		return source;
	}
	public Object download(String src) {
		// TODO Auto-generated method stub
		return null;
	}
}
